//
//  DSLgViewController.m
//  DS爱信
//
//  Created by 秦建英 on 14-6-1.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "DSLgViewController.h"
#import "DSDengViewController.h"
#import "NetAccess.h"
@interface DSLgViewController ()<NSURLConnectionDataDelegate,UITextFieldDelegate>
@end
@implementation DSLgViewController
{
    UITextField *_field;
    UITextField *_field1;
    UITextField *_field2;
    UIAlertView *_alertView;
    NSString *_string;
    NSMutableData *_data;
    NSUserDefaults *users;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        users =[NSUserDefaults standardUserDefaults];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [self textfield];
}
-(void)textfield
{
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainicon.png"]];
    imageView.frame =CGRectMake(40, 50,260, 110);
    [self.view addSubview:imageView];
    _field =[[UITextField alloc]initWithFrame:CGRectMake(20,160, 320-40,40)];
    _field.borderStyle = UITextBorderStyleLine;
    _field.keyboardType =UIKeyboardTypeNumberPad;
    _field.placeholder = @"手机号码:";
    [self.view addSubview:_field];
    
    _field1 =[[UITextField alloc]initWithFrame:CGRectMake(20,210, 320-40,40)];
    _field1.borderStyle =UITextBorderStyleLine;
    _field1.secureTextEntry =YES;
    _field1.delegate =self;
    _field1.placeholder = @"请输入密码:";
    [self.view addSubview:_field1];
    
    _field2 =[[UITextField alloc]initWithFrame:CGRectMake(20,260, 320-40,40)];
    _field2.borderStyle =UITextBorderStyleLine;
    _field2.secureTextEntry =YES;
    _field2.delegate =self;
    _field2.placeholder = @"请再次输入密码:";
    [self.view addSubview:_field2];
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn .frame =CGRectMake(20, _field2.frame.origin.y+90, 280, 40);
    btn.tag =1001;
    [btn setTitle:@"注册" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius =10;
    btn.backgroundColor =[UIColor colorWithRed:0.2f green:0.6f blue:0.9f alpha:1];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1 .frame =CGRectMake(160, btn.frame.origin.y+80, 60, 30);
    btn1.tag =1002;
    [btn1 setTitle:@"登录" forState:UIControlStateNormal];
    btn1.titleLabel.font =[UIFont systemFontOfSize:12];
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn2 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2 .frame =CGRectMake(90, btn.frame.origin.y+80,60, 30);
    btn2.tag =1003;
    btn2.titleLabel.font =[UIFont systemFontOfSize:12];
    [btn2 setTitle:@"温馨提示" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    UIImageView *image =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"x.png"]];
    image.frame =CGRectMake(btn2.frame.origin.x+65, btn.frame.origin.y+85,1, 15);
    [self.view addSubview:image];
}
-(void)btnClick:(UIButton *)btt
{
    if(btt.tag ==1001)
    {
        NSString *message = @"注册成功";
        _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        if(_field.text.length <= 0)
        {
            message = @"对不起亲！您的卡号不存在..";
            _alertView.message = message;
            [_alertView show];
            return;
        }
        if(_field1.text.length <= 0||_field1.text.length<6)
        {
            message = @"亲~密码输入应大于6位哦！";
            _alertView.message = message;
            [_alertView show];
            return;
        }
        if(![_field2.text isEqualToString:_field1.text])
        {
            message = @"亲~密码不正确哦！";
            _alertView.message = message;
            [_alertView show];
            return;
        }
        if(_field.text.length>0&&_field1.text.length>=6&&[_field2.text isEqualToString:_field1.text])
        {
            NetAccess *net = [NetAccess sharedNetAccess];
            if(![net haveNet])
            {
                NSLog(@"网络不可用");
                //没有网络
                UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }else
            {
                NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://182.92.157.109/api/mapp/zc.aspx?a=%@&b=%@&c=2&d=%@",_field.text,@"",_field1.text]];
                NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
                NSURLConnection *connent =[[NSURLConnection alloc]initWithRequest:request delegate:self];
                [connent start];
            }
            return;
        }
    }
    if(btt.tag ==1002)
    {
        [self presentViewController:[DSDengViewController alloc] animated:YES completion:^{
        }];
    }if(btt.tag ==1003)
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请妥善保管好自己的密码,切勿注册过于简单的密码,以防泄露！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
 
    }
}
#pragma mark--网络代理方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _data =[[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    _string =[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",_string);
    if([_string hasPrefix:@"0|1"])
    {
        NSString *message =@"亲！恭喜注册成功哦！快去登录吧！";
        _alertView.message = message;
        [_alertView show];
    }
    else
    {
        _alertView.message = _string;
        [_alertView show];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSString *message =@"亲！网络不给力啊，稍后再试吧..!";
    _alertView.message = message;
    [_alertView show];
    NSLog(@"数据下载失败");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_field resignFirstResponder];
    [_field1 resignFirstResponder];
    [_field2 resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
